﻿using System;
using System.Text;

namespace Uranus.WeiStationAPI.Models
{
    [Serializable]
    public class UserInfo
    {
        public UserInfo()
        {
            this.UserName = string.Empty;
            this.Email = string.Empty;
            this.Mobile = string.Empty;
            this.InvitationCode = string.Empty;
            //this.BirthDate = new DateTime(1900, 0, 0);
        }

        /// <summary>
        /// ID
        /// </summary>
        public int UserID { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        public string FullName { get; set; }

        /// <summary>
        /// 手机号
        /// </summary>
        public string Mobile { get; set; }

        /// <summary>
        /// 手机验证
        /// </summary>
        public int MobileAuthStatusID { get; set; }

        /// <summary>
        /// 身份证
        /// </summary>
        public string IDCard { get; set; }

        /// <summary>
        /// Emaill
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// Email验证码
        /// </summary>
        public int EmailAuthStatusID { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        public int Sex { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        public DateTime BirthDate { get; set; }

        /// <summary>
        /// 邀请码
        /// </summary>
        public string InvitationCode { get; set; }

        /// <summary>
        /// 邀请人ID
        /// </summary>
        public int InvitationUserID { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 身份证验证
        /// </summary>
        public int IDCardAuthStatusID { get; set; }


        public int UserFromID { get; set; }

        /// <summary>
        /// 可用余额
        /// </summary>
        public decimal UsablePrice { get; set; }

        /// <summary>
        /// 定期预期待收
        /// </summary>
        public decimal DQPredictEarning { get; set; }

        /// <summary>
        /// 定期投资总额
        /// </summary>
        public decimal DQAllInvestedPrice { get; set; }

        /// <summary>
        /// 投资金额持有的(在投本金|进入还款中的金额)
        /// </summary>
        public decimal DQInvestingPrice { get; set; }

        /// <summary>
        /// 定期累计收益
        /// </summary>
        public decimal DQEarning { get; set; }

        /// <summary>
        /// 提现冻结金额
        /// </summary>
        public decimal CashFreezePrice { get; set; }

        /// <summary>
        /// 投资冻结金额 (未进入还款中的投资金额)
        /// </summary>
        public decimal InvestFreezePrice { get; set; }

        /// <summary>
        /// 投资次数
        /// </summary>
        public int InvestCount { get; set; }

        public string HuiFuCode { get; set; }

        public string HuiFuName { get; set; }

        /// <summary>
        /// 汇付开通时间--
        /// </summary>
        public DateTime HuiFuCreateTime { get; set; }

        /// <summary>
        /// 登录状态
        /// </summary>
        public bool LoginState { get; set; }

        /// <summary>
        /// 用户额外收益
        /// </summary>
        public decimal ExtraProfit { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string UserPhoto { get; set; }

        /// <summary>
        /// 活期在投本金
        /// </summary>
        public decimal CurrentInvestingPrice { get; set; }

        /// <summary>
        /// 活期昨日收益
        /// </summary>
        public decimal CurrentYesterdayProfit { get; set; }

        /// <summary>
        /// 活期持有收益
        /// </summary>
        public decimal CurrentShortProfit { get; set; }

        /// <summary>
        /// 活期累计收益
        /// </summary>
        public decimal CurrentTotalProfit { get; set; }
    }
    public class UserLoginState
    {
        /// <summary>
        /// 设置用户
        /// </summary>
        /// <param name="UserID"></param>
        public UserLoginState(int UserID)
        {
            this.UserID = UserID;
        }
        /// <summary>
        /// 设置加密串
        /// </summary>
        /// <param name="EnKey"></param>
        public UserLoginState(string EnKey)
        {
            this.EnKey = EnKey;
        }

        private int _userid;
        /// <summary>
        /// 用户id
        /// </summary>
        internal int UserID
        {
            get
            {
                //if (this._userid <= 0) { throw new Exception("userid is 0"); }
                return this._userid;
            }
            set
            {
                this._userid = value;
            }
        }
        private string _ext;
        /// <summary>
        /// 设置的扩展值 随机值 都是数字 6位
        /// </summary>
        internal string Ext
        {
            get
            {
                if (string.IsNullOrEmpty(this._ext))
                {
                    this._ext = new Random().Next(100000, 999999).ToString();
                    return this._ext;
                }
                return this._ext;
            }
            set
            {
                this._ext = value;
            }
        }
        /// <summary>
        /// 加密前的传
        /// </summary>
        private string Key
        {
            get { return string.Format("{0}_{1}", this.UserID, this.Ext); }
        }
        /// <summary>
        /// 密钥
        /// </summary>
        private static readonly string _k = "11122233";
        /// <summary>
        /// 加密后的串 
        /// </summary>
        public string EnKey
        {
            get
            {
                return Uranus.Common.EncryptUtil.Des(this.Key, _k, _k).Replace("-", "");
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("enkey is empty");
                }
                string str = Uranus.Common.EncryptUtil.UnDes(strhandler(value), _k, _k);
                if (str.Contains("出错"))
                {
                    return;
                }
                this.UserID = int.Parse(str.Split('_')[0]);
                this.Ext = str.Split('_')[1];
            }
        }
        /// <summary>
        /// 每隔两位添加一个-
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static string strhandler(string s)
        {
            System.Text.StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                if (i % 2 == 0 && i > 0)
                {
                    stringBuilder.Append("-");
                }
                stringBuilder.Append(s[i]);
            }
            return stringBuilder.ToString();
        }
    }
}